﻿Imports System.Net.Mail
Imports MyDLL

Public Class Form8

    Dim SaveOpen As New MyDLL.SaveOpen

    Private Sub Form8_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.Text = Form1.TextBox10.Text
        TextBox2.Text = Form1.TextBox9.Text
    End Sub

    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        Me.Text = "Email" & " - " & TextBox4.Text
    End Sub

    Private Sub SendToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendToolStripMenuItem.Click
        Try
            Dim FromName As String = TextBox1.Text
            Dim FromAdd As String = TextBox2.Text
            Dim ToAdd As String = TextBox3.Text
            Dim Subject As String = TextBox4.Text
            Dim Body As String = RichTextBox1.Text
            Dim SMTP As String = Form1.TextBox8.Text

            'Start by creating a mail message object
            Dim MyMailMessage As New MailMessage()

            'From requires an instance of the MailAddress type
            MyMailMessage.From = New MailAddress(FromAdd, FromName)

            'To is a collection of MailAddress types
            MyMailMessage.To.Add(ToAdd)

            MyMailMessage.Subject = Subject
            MyMailMessage.Body = Body

            'Add an attachment to your message simply add a new Attachment object to the Attachments collection
            If CheckBox1.Checked Then
                MyMailMessage.Attachments.Add(New Attachment(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml"))
            End If

            If CheckBox2.Checked Then
                MyMailMessage.Attachments.Add(New Attachment(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xaml"))
            End If

            If CheckBox3.Checked Then
                MyMailMessage.Attachments.Add(New Attachment(Form1.TextBox7.Text & Form1.TextBox3.Text & ".rtf"))
            End If

            If CheckBox4.Checked Then
                MyMailMessage.Attachments.Add(New Attachment(Form1.TextBox7.Text & Form1.TextBox3.Text & ".txt"))
            End If

            If CheckBox5.Checked Then
                MyMailMessage.Attachments.Add(New Attachment(Form1.TextBox7.Text & Form1.TextBox3.Text & ".t.xml"))
            End If

            'Create the SMTPClient object and specify the SMTP server name or IP Address
            Dim SMTPServer As New SmtpClient(SMTP)

            SMTPServer.Send(MyMailMessage)
            MsgBox("Email Sent!")
        Catch ex As Exception
            MsgBox("Email Sending Failed!")
        End Try
    End Sub

    Private Sub CheckAttachmentAvailabilityToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckAttachmentAvailabilityToolStripMenuItem.Click
        If My.Computer.FileSystem.FileExists(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml") Then
            CheckBox1.CheckState = CheckState.Checked
        Else
            CheckBox1.CheckState = CheckState.Unchecked
        End If

        If My.Computer.FileSystem.FileExists(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xaml") Then
            CheckBox2.CheckState = CheckState.Checked
        Else
            CheckBox2.CheckState = CheckState.Unchecked
        End If

        If My.Computer.FileSystem.FileExists(Form1.TextBox7.Text & Form1.TextBox3.Text & ".rtf") Then
            CheckBox3.CheckState = CheckState.Checked
        Else
            CheckBox3.CheckState = CheckState.Unchecked
        End If

        If My.Computer.FileSystem.FileExists(Form1.TextBox7.Text & Form1.TextBox3.Text & ".txt") Then
            CheckBox4.CheckState = CheckState.Checked
        Else
            CheckBox4.CheckState = CheckState.Unchecked

        End If

        If My.Computer.FileSystem.FileExists(Form1.TextBox7.Text & Form1.TextBox3.Text & ".t.xml") Then
            CheckBox5.CheckState = CheckState.Checked
        Else
            CheckBox5.CheckState = CheckState.Unchecked

        End If
    End Sub

    Function MakeXml() As XElement
        Dim ToAdd As String = TextBox3.Text
        Dim Subject As String = TextBox4.Text
        Dim Body As String = RichTextBox1.Text

        Dim xml As String = CheckBox1.CheckState.ToString
        Dim xaml As String = CheckBox2.CheckState.ToString
        Dim rtf As String = CheckBox3.CheckState.ToString
        Dim txt As String = CheckBox4.CheckState.ToString
        Dim txml As String = CheckBox5.CheckState.ToString

        Dim Place As String = Form1.ComboBox1.SelectedItem
        Dim Drive As String = Form1.TextBox1.Text
        Dim Folders As String = Form1.TextBox2.Text
        Dim FileName As String = Form1.TextBox3.Text

        Dim Email As XElement = _
        <Email>
            <Message>
                <To><%= ToAdd %></To>
                <Subject><%= Subject %></Subject>
                <Body><%= Body %></Body>
            </Message>
            <Attachments>
                <Address>
                    <Place><%= Place %></Place>
                    <Drive><%= Drive %></Drive>
                    <Folders><%= Folders %></Folders>
                    <File><%= FileName %></File>
                </Address>
                <Types>
                    <xml><%= xml %></xml>
                    <xaml><%= xaml %></xaml>
                    <rtf><%= rtf %></rtf>
                    <txt><%= txt %></txt>
                    <txml><%= txml %></txml>
                </Types>
            </Attachments>
        </Email>

        Return Email
    End Function

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click

        Dim Msg As String = MakeXml().ToString


        If ComboBox1.SelectedItem = "Drive" Then
            TextBox7.Text = TextBox8.Text & ":\" & TextBox6.Text & "\"
        End If
        If ComboBox1.SelectedItem = "Desktop" Then
            TextBox7.Text = My.Computer.FileSystem.SpecialDirectories.Desktop & "\" & TextBox6.Text & "\"
        End If
        If ComboBox1.SelectedItem = "My Documents" Then
            TextBox7.Text = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & TextBox6.Text & "\"
        End If

        Dim Folder As String = TextBox7.Text

        If My.Computer.FileSystem.DirectoryExists(Folder) Then

            Try
                Dim Save As String = Folder & "\" & TextBox5.Text & ".xml"


                My.Computer.FileSystem.WriteAllText(Save, Msg, False)



            Catch
                MsgBox("Please enter a File Name and Extension")
            End Try
        Else

            Try

                My.Computer.FileSystem.CreateDirectory(Folder)

                Dim Save As String = Folder & "\" & TextBox5.Text & ".xml"


                My.Computer.FileSystem.WriteAllText(Save, Msg, False)



            Catch
                MsgBox("Please enter a File Name")
            End Try

        End If
    End Sub
End Class